package com.nova.edc.service.system.impl;

import com.nova.edc.dao.system.DeviceLightDao;
import com.nova.edc.model.system.DeviceLight;
import com.nova.edc.model.system.ModelConstant;
import com.nova.edc.model.util.ExcelArea;
import com.nova.edc.service.system.DeviceLightService;
import com.nova.frame.dao.buzz.query.Order;
import com.nova.frame.dao.buzz.query.Query;
import com.nova.frame.dao.criterion.Restrictions;
import com.nova.frame.dao.pager.Page;
import com.nova.frame.dao.pager.PageList;
import com.nova.frame.utils.StringUtils;
import com.nova.module.filter.query.Filter;
import com.seio.edc.util.IntegerUtils;
import org.apache.commons.digester.RegexRules;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

public class DeviceLightServiceImpl implements DeviceLightService {

    @Autowired
    private DeviceLightDao novaDeviceLightDao;

    @Override
    public List<DeviceLight> findLightEnergy(int lightType) {
        return novaDeviceLightDao.query()
                .add(Restrictions.eq("t_device_light.LightType", lightType))
                .list();
    }

    @Override
    public PageList<DeviceLight> findPageLightEnergy(DeviceLight lightEnergy,
                                                     Page page, String sord, String sidx) {
        Query<DeviceLight> query = novaDeviceLightDao.query().add(
                Restrictions.eq("Scrapped", 0));
        query.addOrder(Order.custom(sidx, sord));
        Filter.addFilterCondition(query, lightEnergy);
        return query.pageList(page);
    }

    @Override
    public List<DeviceLight> findLightEnergy(DeviceLight lightEnergy,
                                             String sord, String sidx) {
        Query<DeviceLight> query = novaDeviceLightDao.query();
        query.addOrder(Order.custom(sidx, sord));
        Filter.addFilterCondition(query, lightEnergy);
        return query.list();
    }

    @Override
    public List<String> getLightCodeByList(List<DeviceLight> lightEnergies) {
        List<String> codes = new ArrayList<String>();
        for (int i = 0; i < lightEnergies.size(); i++) {
            codes.add(lightEnergies.get(i).getLightcode());
        }
        return codes;
    }

    @Override
    public boolean isExistCode(String lightCode, Integer lightType) {
        Query<DeviceLight> query = novaDeviceLightDao.query()
                .add(Restrictions.eq("t_device_light.LightCode", lightCode))
                .add(Restrictions.eq("t_device_light.LightType", lightType));
        return query.list().size() > 0;
    }
    
    @Override
    public void trimLightEnergy(DeviceLight lightEnergy) {
        if (StringUtils.isNotNullOrEmpty(lightEnergy.getLightname())) {
            lightEnergy.setLightname(lightEnergy.getLightname().trim());
        }
        if (StringUtils.isNotNullOrEmpty(lightEnergy.getLightcode())) {
            lightEnergy.setLightcode(lightEnergy.getLightcode().trim());
        }
    }

    @Override
    public DeviceLight getById(Integer id) {
        return novaDeviceLightDao.getById(id);
    }

    @Override
    public DeviceLight insert(DeviceLight lightEnergy) {
        List<DeviceLight> deviceLightList = novaDeviceLightDao.query()
                .add(Restrictions.eq("LightCode", lightEnergy.getLightcode()))
                .add(Restrictions.eq("LightType", lightEnergy.getLighttype())).list();
        if (deviceLightList != null && deviceLightList.size() > 0) {
            return deviceLightList.get(0);
        } else {
            return novaDeviceLightDao.insert(lightEnergy);
        }
    }

    @Override
    public int update(DeviceLight lightEnergy) {
        return novaDeviceLightDao.updateNotNull(lightEnergy);
    }

    @Override
    public int deleteById(Integer id) {
        return novaDeviceLightDao.deleteById(id);
    }

    @Override
    public void insertByExcel(List<ExcelArea> list) {
        for (ExcelArea excelArea : list) {
            if (ModelConstant.DEVICE_LIGHTENERGY.equals(excelArea.getDevicetypename()) && excelArea.getReallighttype() != null) {
                DeviceLight traLight = new DeviceLight();
                traLight.setLightcode(excelArea.getTralightcode());
                traLight.setLightname(excelArea.getTralightname());
                traLight.setPower(excelArea.getTrapower());
                traLight.setLighttype(ModelConstant.TRA_LIGHTTYPE);
                if (com.nova.module.util.StringUtils.isNotNullOrEmpty(excelArea.getTralightcode().trim()) && IntegerUtils.isEmptyAndNull(excelArea.getTrapower())) {
                    excelArea.setTralightid(insert(traLight).getId());
                }
                DeviceLight ledLight = new DeviceLight();
                ledLight.setLightcode(excelArea.getLedlightcode());
                ledLight.setLighttype(ModelConstant.LED_LIGHTTYPE);
                ledLight.setLightname(excelArea.getLedlightname());
                ledLight.setPower(excelArea.getLedpower());
                if (com.nova.module.util.StringUtils.isNotNullOrEmpty(excelArea.getLedlightcode()) && IntegerUtils.isEmptyAndNull(excelArea.getLedpower())) {
                    excelArea.setLedlightid(insert(ledLight).getId());
                }
            }
        }
    }

    @Override
    public List<DeviceLight> findByCode(String lightCode, Integer lightType) {
        Query<DeviceLight> query = novaDeviceLightDao.query()
                .add(Restrictions.eq("t_device_light.LightCode", lightCode))
                .add(Restrictions.eq("t_device_light.LightType", lightType));
        return query.list();
    }
}
